
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nus.application.web.user;

import com.nus.application.ejb.user.UserEjbLocal;
import com.nus.application.web.base.BaseAction;
import com.nus.application.web.user.form.UserMaintenanceForm;
import com.nus.dataaccess.dao.role.RoleType;
import com.nus.dataaccess.dao.role.dao.RoleTypeDao;
import com.nus.dataaccess.dao.user.UserInfo;
import com.nus.dataaccess.dao.user.dao.UserInfoDao;
import com.nus.dataaccess.dao.util.HibernateUtil;
import com.opensymphony.xwork2.ValidationAware;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

/**
 *
 * @author robin.foe
 */
public class UserMaintenance extends BaseAction implements ValidationAware {

    @Inject
    UserEjbLocal userEjb;
    public String doList() {
        this.checkRoleType();
        this.form.setFullListSize(userEjb.countPaginateUserInfo(null));
        this.form.setPageNumber(super.getPage());
        System.err.println(this.getForm().getPageNumber());
        this.getForm().setUserInfos(
                userEjb.paginateUserInfo(form.calculateStartRow(super.getPage()), form.getObjectsPerPage(), null, false, null));
        return SUCCESS;
    }
    public String doMaintain() {
        this.checkRoleType();
        //String id = ServletActionContext.getRequest().getParameter("id");
        //this.getForm().setUserInfo(UserInfoDao.getInstance().getUserInfoById(Long.parseLong(id)));
        return SUCCESS;
    }
    public String doCreate() {
        this.checkRoleType();
        this.getForm().setUserInfo(new UserInfo());
        return SUCCESS;
    }
    public void checkRoleType() {
        RoleTypeDao roleTypeDao = RoleTypeDao.getInstance();
        if (roleTypeDao.findAll().size() == 0) {
            try {
                HibernateUtil.beginTransaction();
                RoleType roleType;
                roleType = new RoleType();
                roleType.setCode("admin");
                roleType.setDescription("Admin");
                roleTypeDao.save(roleType);

                roleType = new RoleType();
                roleType.setCode("author");
                roleType.setDescription("Author");
                roleTypeDao.save(roleType);

                roleType = new RoleType();
                roleType.setCode("participant");
                roleType.setDescription("Participant");
                roleTypeDao.save(roleType);
                HibernateUtil.commitTransaction();
            } catch (Exception e) {
                HibernateUtil.rollbackTransaction();
                e.printStackTrace();
            } finally {

                try {
                    HibernateUtil.closeSession();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

    }
    public String doSave() {
        String[] roleTypeId = ServletActionContext.getRequest().getParameterValues("userRoleId");
        if (roleTypeId == null) {
            System.out.println("user roles = null");
        } else {
            List<RoleType> roleTypes = new ArrayList<RoleType>();
            for (int i=0; i<roleTypeId.length; i++) {
                RoleType roleType = RoleTypeDao.getInstance().getByRoleTypeId(Long.parseLong(roleTypeId[i]));
                roleTypes.add(roleType);
            }
            this.getForm().getUserInfo().getUserAccount().setRoleTypes(roleTypes);

        }
        userEjb.persistUser(this.getForm().getUserInfo());
        super.addActionMessage("Information successfully saved");
        return SUCCESS;
    }
    private UserMaintenanceForm form = new UserMaintenanceForm();
    public UserMaintenanceForm getForm() {
        return form;
    }
    public void setForm(UserMaintenanceForm form) {
        this.form = form;
    }
    @Override
    public void validate() {

        if (!super.isSaveEvent()) {
            if (super.isListEvent()) {
                super.clearActionErrors();
            }

            return;
        }

        if (StringUtils.isEmpty(this.getForm().getUserInfo().getFullName())) {
            super.addActionError(super.getText("error.full_name_is_required"));
        }

        if (StringUtils.isEmpty(this.getForm().getUserInfo().getUserAccount().getUsername())) {
            super.addActionError(super.getText("error.username_is_required"));
        }

        if (StringUtils.isEmpty(this.getForm().getUserInfo().getUserAccount().getPassword())) {
            super.addActionError(super.getText("error.password_is_required"));
        }

        if (!super.hasActionErrors()) {
            // validate username
            if (userEjb.retrieveUserByUsername(this.getForm().getUserInfo().getUserAccount().getUsername()) != null
                    && userEjb.retrieveUserByUsername(this.getForm().getUserInfo().getUserAccount().getUsername()).getId() != this.getForm().getUserInfo().getUserAccount().getId()) {
                super.addActionError(super.getText("error.username_already_taken"));
            }

        }
    }
}
